package cn.cheegon.blog.common.exception;

import cn.cheegon.blog.common.api.CommonResult;
import cn.cheegon.blog.common.api.ResultCode;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;



/**
 * @Desc:全局异常处理 @ControllerAdvice
 * @Author: cheegon
 * @Date: Create in 2024/6/7  下午2:03
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = ApiException.class)
    public CommonResult handle(ApiException e) {
        logger.error("api异常！:", e);
        if (e.getErrorCode() != null) {
            return CommonResult.failed(e.getErrorCode());
        }
        return CommonResult.failed(e.getMessage());
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public CommonResult handleValidException(MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        String message = null;
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            if (fieldError != null) {
                message = fieldError.getDefaultMessage();
            }
        }
        logger.error("方法参数异常！:", e);
        return CommonResult.validateFailed(message);
    }

    @ExceptionHandler(value = BindException.class)
    public CommonResult handleValidException(BindException e) {
        BindingResult bindingResult = e.getBindingResult();
        String message = null;
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            if (fieldError != null) {
                message = fieldError.getField() + fieldError.getDefaultMessage();
            }
        }
        logger.error("Bind异常！:", e);
        return CommonResult.validateFailed(message);
    }

    @ExceptionHandler(NullPointerException.class)
    public CommonResult nullPointException(NullPointerException e) {
        logger.error("空指针异常！:", e);
        return CommonResult.failed(ResultCode.NULL_POINTER_ERROR, e.getMessage());
    }

    @ExceptionHandler(DataAccessException.class)
    public CommonResult sqlException(DataAccessException e) {
        logger.error("数据库异常！:", e);
        return CommonResult.failed(ResultCode.SQL_ERROR);
    }

    @ExceptionHandler(Exception.class)
    public CommonResult exception(HttpServletRequest req, Exception e) {
        logger.error("未知异常！:", e);
        return CommonResult.failed(ResultCode.OTHER_ERROR, "服务器内部错误");
    }

}
